package priv.zgh.forum.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import priv.zgh.forum.auth.entity.Power;
import priv.zgh.forum.auth.entity.RolePower;
import priv.zgh.forum.auth.entity.RoleUser;
import priv.zgh.forum.auth.entity.User;
import priv.zgh.forum.auth.mapper.UserMapper;
import priv.zgh.forum.auth.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import priv.zgh.forum.utils.MD5;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZGH
 * @since 2021-10-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    RoleService roleService;
    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
    RoleUserService roleUserService;
    @Autowired
    public void setRoleUserService(RoleUserService roleUserService) {
        this.roleUserService = roleUserService;
    }
    PowerService powerService;
    @Autowired
    public void setPowerService(PowerService powerService) {
        this.powerService = powerService;
    }
    RolePowerService rolePowerService;
    @Autowired
    public void setRolePowerService(RolePowerService rolePowerService) {
        this.rolePowerService = rolePowerService;
    }

    @Override
    public String login(User user) {
        // 将密码取出进行md5加密再与数据库中的密码字段比较
        System.out.println(user.getPassword());
        String password = MD5.encrypt(user.getPassword());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",password);
        List<User> userList = baseMapper.selectList(wrapper);
        if(userList.size() > 0){
            return userList.get(0).getId();
        }else{
            return null;
        }
    }

    @Override
    public int register(User user) {
        // 将传过来的密码进行md5加密存库
        user.setPassword(MD5.encrypt(user.getPassword()));
        return baseMapper.insert(user);
    }

    @Override
    public User selectUserInfoByEmail(String email) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        return baseMapper.selectList(wrapper).get(0);
    }

    @Override
    public List<String> getUserAllPowerPath(User user) {
        List<String> powerPathList = new ArrayList<>();
        // 先获取用户所对应角色ID
        QueryWrapper<RoleUser> roleUserQueryWrapper = new QueryWrapper<>();
        roleUserQueryWrapper.eq("user_id",user.getId());
        List<RoleUser> roleUserList = roleUserService.list(roleUserQueryWrapper);
        // 遍历角色ID查询权限信息
        for (RoleUser roleUser : roleUserList){
            // 根据角色ID获取对应的角色所对应的所有权限ID
            QueryWrapper<RolePower> rolePowerQueryWrapper = new QueryWrapper<>();
            rolePowerQueryWrapper.eq("role_id",roleUser.getRoleId());
            List<RolePower> rolePowerList = rolePowerService.list(rolePowerQueryWrapper);
            // 遍历权限ID来获取权限路径信息
            for(RolePower rolePower : rolePowerList){
                Power power = powerService.getById(rolePower.getPowerId());
                powerPathList.add(power.getPath());
            }
            // 如果没有权限路径就设置默认权限
            if (powerPathList.size() <= 0) {
                System.out.println("用户ID:" + user.getId() + ",没有设置权限,被赋予默认权限.");
                powerPathList.add("/*");
            }
            return powerPathList;
        }
        return null;
    }


}
